import React from 'react'
import MainContext, { initState, reduce } from './MainContext';

class Provider extends React.Component {

  constructor(props) {
    super(props);

    // State 也包含了更新函数，因此它会被传递进 context provider。
    this.state = {
      ...initState,
    };

    for (const key in reduce) {
      this.state[key] = (param) => {
        console.log("change  ==============>", param )
        this.setState(reduce[key](param));
      }
    }
  }

  render() {
    console.log("complete ==============>", this.state )
    return (
      <MainContext.Provider value={this.state} >
        {this.props.children}
      </MainContext.Provider>
    )
  }

}

export default Provider;
